package com.hsytech.dijiebao.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.base.BaseActivity;
import com.hsytech.dijiebao.home.adapter.MoreMyselfHelpHotleAdapter;
import com.hsytech.dijiebao.home.alldetail.HotelDetailActivity;
import com.hsytech.dijiebao.home.alldetail.MyselfHotelDetailActivity;
import com.hsytech.dijiebao.utils.HttpUtil;
import com.hsytech.dijiebao.utils.SharedPreferencesTools;
import com.hsytech.dijiebao.view.pulltorefresh.PullToRefreshBase;
import com.hsytech.dijiebao.view.pulltorefresh.PullToRefreshBase.Mode;
import com.hsytech.dijiebao.view.pulltorefresh.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;

public class MoreActivity extends BaseActivity {
    private PullToRefreshListView listview_more_taxi;
    private TextView bar_title_tv, default_text;
    private MoreMyselfHelpHotleAdapter taxiAdapter;
    private List<HashMap<String, String>> listdatat = new ArrayList<HashMap<String, String>>();
    private ImageButton top_back_btn;
    private Button bar_right_top_btn;
    private View root;
    private Inflater inflater;
    private int firstRow = 0;
    private int listRows = 10;
    private int currentNum = 0;
    private String count;
    private DPHttpUtil.HttpCallback callBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            JSONObject result = null;
            try {
                result = new JSONObject(response);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            count = result.optString("count");
            JSONArray resultData = result.optJSONArray("data");
            if (0 == what) {
                for (int i = 0; i < resultData.length(); i++) {
                    String cityName = resultData.optJSONObject(i).optString("city_name");
                    String areaName = resultData.optJSONObject(i).optString("area_name");
                    String hotleName = resultData.optJSONObject(i).optString("hotel_cat_name");
                    String name = resultData.optJSONObject(i).optString("name");
                    String startName = resultData.optJSONObject(i).optString("start_time");
                    String endName = resultData.optJSONObject(i).optString("end_time");
                    String id = resultData.optJSONObject(i).optString("id");
                    String check_in_num = resultData.optJSONObject(i).optString("check_in_num");
                    String publisher_name = resultData.optJSONObject(i).optString("publisher_name");
                    HashMap<String, String> taxiList = new HashMap<String, String>();
                    // taxiList = listdatat.get(i);
                    taxiList.put("city", cityName);
                    taxiList.put("id", id);
                    taxiList.put("publisher_name", getString(R.string.base_fa_bu_ren) + " " + publisher_name);
                    taxiList.put("my_hot", resultData.optJSONObject(i).optString("is_hot"));
                    taxiList.put("my_new", resultData.optJSONObject(i).optString("is_new"));
                    taxiList.put("recommender", resultData.optJSONObject(i).optString("is_recommend"));
                    taxiList.put("content", hotleName + " " + startName + "-" + endName + " " + getString(R.string.base_ru_zhu) + check_in_num + getString(R.string.base_ren));
                    listdatat.add(taxiList);
                }
                taxiAdapter.notifyDataSetChanged();
                listview_more_taxi.onRefreshComplete();
                currentNum = currentNum + 10;
            } else {

                for (int i = 0; i < resultData.length(); i++) {
                    String cityName = resultData.optJSONObject(i).optString("city_name");
                    String areaName = resultData.optJSONObject(i).optString("area_name");
                    String carName = resultData.optJSONObject(i).optString("car_cat_name");
                    String name = resultData.optJSONObject(i).optString("name");
                    String startName = resultData.optJSONObject(i).optString("start_time");
                    String endName = resultData.optJSONObject(i).optString("end_time");
                    String id = resultData.optJSONObject(i).optString("id");
                    String carry_passenger_num = resultData.optJSONObject(i).optString("carry_passenger_num");
                    String publisher_name = resultData.optJSONObject(i).optString("publisher_name");
                    HashMap<String, String> taxiList = new HashMap<String, String>();
                    // taxiList = listdatat.get(i);
                    taxiList.put("city", cityName);
                    taxiList.put("id", id);
                    taxiList.put("publisher_name", getString(R.string.base_fa_bu_ren) + " " + publisher_name);
                    taxiList.put("content",
                            carName + " " + startName + "-" + endName + " " + getString(R.string.base_chen_che) + carry_passenger_num + getString(R.string.base_ren));
                    taxiList.put("my_hot", resultData.optJSONObject(i).optString("is_hot"));
                    taxiList.put("my_new", resultData.optJSONObject(i).optString("is_new"));
                    taxiList.put("recommender", resultData.optJSONObject(i).optString("is_recommend"));
                    listdatat.add(taxiList);
                    //
                }
                taxiAdapter.notifyDataSetChanged();
                listview_more_taxi.onRefreshComplete();
                currentNum = currentNum + 10;

            }


        }

        @Override
        public void onError(int what, int type, VolleyError error) {

            listview_more_taxi.onRefreshComplete();

        }
    };
    private Intent intent;
    private String title, type, url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_myself_hotle);
        intent = getIntent();
        title = intent.getStringExtra("title");
        type = intent.getStringExtra("type");
        url = intent.getStringExtra("url");
        initView();
        getHttp();
    }

    private void initView() {
        initTitle();
        initBody();
    }

    private void getHttp() {

        Map<String, String> map = new HashMap<>();
        map.put("type", type);
        if (!TextUtils.isEmpty(SharedPreferencesTools.getTOKEN(this))) {
            map.put("token", SharedPreferencesTools.getTOKEN(this));
        }
        map.put("firstRow", firstRow + "");
        map.put("listRows", listRows + "");
        if (!TextUtils.isEmpty(SharedPreferencesTools.getCityID(this))) {
            map.put("city_ids", SharedPreferencesTools.getCityID(this));
        }
        if (title.contains("车")) {
            HttpUtil.httpGet(this, 1, url + "?type=" + type + "&firstRow=" + firstRow + "&listRows=" + listRows + "&city_ids=" + SharedPreferencesTools.getCityID(this), callBack);
        } else {
            HttpUtil.httpPostWithParams(this, 0, url, map, callBack);
        }


    }

    private void initBody() {
        listview_more_taxi = (PullToRefreshListView) findViewById(R.id.listview_more_taxi);
        taxiAdapter = new MoreMyselfHelpHotleAdapter(this, listdatat);
        listview_more_taxi.setAdapter(taxiAdapter);
        listview_more_taxi.setMode(Mode.BOTH);
        listview_more_taxi.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                firstRow = 0;
                listRows = 10;
                listdatat.clear();
                taxiAdapter.notifyDataSetChanged();
                getHttp();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (currentNum == 0) {
                    listview_more_taxi.onRefreshComplete();
                    return;
                }
                if (currentNum >= Integer.valueOf(count)) {
                    // showToast("û�и���������");

                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            listview_more_taxi.onRefreshComplete();

                        }
                    }, 500);
                }
                firstRow = firstRow + listRows;
                getHttp();
            }
        });
        listview_more_taxi.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent();
                intent.setClass(MoreActivity.this, HomeDetailActivity.class);
                intent.putExtra("id", listdatat.get(position - 1).get("id"));
                if (title.contains("车")) {
                    intent.putExtra("type", "2");
                } else {
                    intent.putExtra("type", "1");
                }
                startActivity(intent);
            }
        });

    }

    private void initTitle() {
        top_back_btn = (ImageButton) findViewById(R.id.top_back_btn);
        top_back_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        bar_right_top_btn = (Button) findViewById(R.id.bar_right_top_btn);
        bar_right_top_btn.setVisibility(View.GONE);
        bar_title_tv = (TextView) findViewById(R.id.bar_title_tv);
        bar_title_tv.setText(title);
    }
}
